<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/10
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\store;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;

use ImiApp\ApiServer\Api\Model\Goodssku;

use ImiApp\ApiServer\Api\Model\Goods as FaGoods;
use ImiApp\ApiServer\Api\Model\GoodsskuPrice;
// use ImiApp\ApiServer\Common\Store\StoreAuth;
// * @StoreAuth(name="token",nologin={"no"})

/**
 * 拍卖
 * @Auth(name="token",nologin={"get_auction_end","get_auction_advance_show"})
 * @Controller("/store.auction/")
 */
class Auction extends StoreOwner
{


    // protected $whereCom;



    // public function _initialize()
    // {
    //     parent::_initialize();
    //     $this->whereCom['store_id'] = $this->store_id;
    // }

    /**
     * 获取拍卖结束时间
     *
     * @return void
     * @Action
     */
    public function get_auction_end()
    {
        $list = Db::name('auction_end')->orderRaw('s asc')->select()->getArray();
        return $this->success('结束时间', $list);
    }

    /**
     * 获取拍卖预展览时间
     *
     * @return void
     * @Action
     */
    public function get_auction_advance_show()
    {
        $list = Db::name('auction_advance_show')->orderRaw('s asc')->select()->getArray();
        return $this->success('预展览时间', $list);
    }





    /**
     * 贝壳竞拍
     *
     * @param string $start_status 开始类型:time=时间,number=人数,
     * @param int $enroll_num 报名人数'
     * @param int $goods_id 商品',
     * @param string $goods_sku_price_ids 商品规格',
     * @param float $start_price 起拍价格',
     * @param float $minimum_price 最低价格(保留价)',
     * @param float $inc_price1 加幅度1
     * @param float $inc_price2 加幅度2
     * @param float $inc_price3 加幅度3
     * @param float $inc_price4 加幅度4
     * @param float $buyer_surety 买家保证金',
     * @param float $shell_multiple 贝壳一次(倍数)',
     * @param float $enroll_score 报名需要积分',
     * @param float $enroll_out_perk_score 报名出局积分补贴
     * @param int $auction_advance_show_id 预展示id
     * @param int $auction_end_id 结束时间id
     * @param string $cycle 几个周期',
     * @param string $dispatch_ids 发货模板'
     * @param int $enroll_num 开拍最低人数'人数必传
     * 
     * 
     * @param float $store_price 店铺价
     * @param string $surety_buy 保价买:no=不支持,yes=支持
     * 
     * @return void
     * @Action
     */
    public function create1(
        $start_status = null, // 开始类型:time=时间,number=人数,
        $goods_id = null, // 商品',
        $goods_sku_price_ids = null, // 商品规格',
        ?float $start_price = null, // 起拍价格',
        ?float $minimum_price = null, // 最低价格(保留价)',

        ?float $enroll_out_perk_score = null, //报名出局积分补贴
        // ?float $plus_price = null, // 加价幅度',
        // ?float $out_perk_shell = null, // 出局贝壳补贴',
        ?float $shell_multiple = null, // 贝壳一次(倍数)',
        ?float $enroll_score = null, // 报名需要积分',
        // ?float $max_price = null, // 最高成交价',
        ?int $auction_advance_show_id = null, //预展示id
        ?int $auction_end_id = null, //结束时间id
        // ?int $advance_show_s = null, // 预展示多少s',
        // ?int $end_s = null, // 结束时长',
        ?int $cycle = null, // 几个周期',
        // ?string $end_time = null,
        ?int $enroll_num = null,

        ?float $inc_price1 = null,
        ?float $inc_price2 = null,
        ?float $inc_price3 = null,
        ?float $inc_price4 = null,
        ?float $store_price = null,
        $surety_buy = 'no',

        $dispatch_ids = null // 发货模板',

    ) {
        //     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        // * @param int $advance_show_s 预展示多少s',
        // * @param string $end_time 结束时间'人数必传
        // * @param int $end_s 结束时长',
        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        // $param = $this->request->post();
        $type_status = 1;
        switch ($type_status) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_贝壳竞拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '2':
                $拍卖类型 = '降价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_降价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '3':
                $拍卖类型 = '升价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_升价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_降)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_升)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            default:
                # code...
                break;
        }

        //发布

        //判断规格的库存是否都存在

        //判断卖家的保证金 = 买家的保证金*1.5倍(可设置)

        //保留价格

        //发布后需要审核

        $goods = $this->GetGoodsDataById($goods_id);
        if ($goods['store_id'] != $this->store_id) {
            return $this->error('本店没有该商品');
        }


        $data['surety_buy'] = $surety_buy ?? 'no';
        $data['store_price'] = $goods['store_price'];
        $data['title'] = $goods['title'];
        $data['store_id'] = $this->store_id;
        $data['type_status'] = $type_status;
        $data['start_status'] = $start_status; //开始类型:time=时间,number=人数
        $data['goods_id'] = $goods_id;
        $data['goodscate_id'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->value('pid');
        if(!$goods_sku_price_ids){
            $goods_sku_price_ids = $this->获取商品规格列表ids($goods_id);
        }
        $data['goods_sku_price_ids'] = $goods_sku_price_ids;

        $this->验证商品是否含有这个库存规格($goods_id, $goods_sku_price_ids);


        $goods_sku_price_arr = explode(',', $goods_sku_price_ids);
        //判断库存是否充足  发布后需要减少库存
        $res = $this->isGoodsskuPriceStockIds($goods_sku_price_arr);
        if ($res != 'ok') {
            return $this->error('库存异常', '', CODE_库存不足);
        }

        $data['enroll_out_perk_score'] = $enroll_out_perk_score;
        $data['start_price'] = $start_price;
        $data['now_price'] = $start_price;
        // $data['auto_now_price'] = $start_price;
        $data['minimum_price'] = $minimum_price;


        if ($start_status == 'number') {
            if ($enroll_num < 1) {
                return $this->error('最低1人开拍');
            }
        }

        $data['inc_price1'] = $inc_price1;
        $data['inc_price2'] = $inc_price2;
        $data['inc_price3'] = $inc_price3;
        $data['inc_price4'] = $inc_price4;

        $data['enroll_num'] = $enroll_num;
        // $data['out_perk_score'] = $out_perk_score;
        $data['shell_multiple'] = $shell_multiple;
        $data['enroll_score'] = $enroll_score;

        $data['auction_advance_show_id'] = $auction_advance_show_id;
        $advance_show_s = Db::name('auction_advance_show')->where('id', '=', $auction_advance_show_id)->value('s');
        $data['auction_end_id'] = $auction_end_id;
        $end_s = Db::name('auction_end')->where('id', '=', $auction_end_id)->value('s');

        $data['advance_show_s'] = $advance_show_s; //预展示多少s
        $data['end_s'] = $end_s;

        $data['cycle'] = $cycle;
        $data['dispatch_ids'] = $dispatch_ids;
        $data['createtime'] = time();


        $id = Db::name('auction')->insert($data)->getLastInsertId();

        return $this->success('等待审核');
    }





    /**
     * 创建降价拍
     *
     * @param string $start_status 开始类型:time=时间,number=人数,
     * @param int $enroll_num 报名人数'
     * @param int $goods_id 商品',
     * @param string $goods_sku_price_ids 商品规格',
     * @param float $start_price 起拍价格',
     * @param float $minimum_price 最低价格(保留价)',
     * @param float $dec_price1 减幅度1
     * @param float $dec_price2 减幅度2
     * @param float $dec_price3 减幅度3
     * @param float $dec_price4 减幅度4
     * @param float $buyer_surety 买家保证金',
     * @param float $passin_plus_price 流拍后加价',
     * @param float $enroll_shell 报名需要贝壳',
     * @param int $auction_advance_show_id 预展示id
     * @param int $auction_end_id 结束时间id
     * @param string $cycle 几个周期',
     * @param string $dispatch_ids 发货模板'
     * @param int $enroll_num 开拍最低人数'人数必传
     * 
     * @param float $plus_price 加价幅度
     * @param int $timer_dec_s 多少秒降一次价格
     * @return void
     * @Action
     */
    public function create2(
        $start_status = null, // 开始类型:time=时间,number=人数,
        $goods_id = null, // 商品',
        $goods_sku_price_ids = null, // 商品规格',
        ?float $start_price = null, // 起拍价格',
        ?float $minimum_price = null, // 最低价格(保留价)',
        ?float $buyer_surety = null, // 买家保证金',
        ?int $timer_dec_s = null, //多少秒降一次价格
        ?float $plus_price = null, // 加价幅度',
        // ?float $out_perk_shell = null, // 出局贝壳补贴',
        ?float $passin_plus_price = null, // 流拍后加价',
        ?float $enroll_shell = null, // 报名需要贝壳',
        // ?float $max_price = null, // 最高成交价',
        ?int $auction_advance_show_id = null, //预展示id
        ?int $auction_end_id = null, //结束时间id
        // ?int $advance_show_s = null, // 预展示多少s',
        // ?int $end_s = null, // 结束时长',
        ?int $cycle = null, // 几个周期',
        // ?string $end_time = null,
        ?int $enroll_num = null,

        ?float $dec_price1 = null,
        ?float $dec_price2 = null,
        ?float $dec_price3 = null,
        ?float $dec_price4 = null,

        $dispatch_ids = null // 发货模板',

    ) {
        //     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        // * @param int $advance_show_s 预展示多少s',
        // * @param string $end_time 结束时间'人数必传
        // * @param int $end_s 结束时长',
        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        // $param = $this->request->post();
        $type_status = 2;
        switch ($type_status) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_贝壳竞拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '2':
                $拍卖类型 = '降价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_降价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '3':
                $拍卖类型 = '升价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_升价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_降)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_升)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            default:
                # code...
                break;
        }

        //发布

        //判断规格的库存是否都存在

        //判断卖家的保证金 = 买家的保证金*1.5倍(可设置)

        //保留价格

        //发布后需要审核

        $goods = $this->GetGoodsDataById($goods_id);
        if ($goods['store_id'] != $this->store_id) {
            return $this->error('本店没有该商品');
        }

        if ($timer_dec_s < 10) {
            return $this->error('设置降价时间太短' . $timer_dec_s);
        }

        $data['title'] = $goods['title'];
        $data['store_id'] = $this->store_id;
        $data['type_status'] = $type_status;
        $data['start_status'] = $start_status; //开始类型:time=时间,number=人数
        $data['goods_id'] = $goods_id;
        $data['goodscate_id'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->value('pid');
        if(!$goods_sku_price_ids){
            $goods_sku_price_ids = $this->获取商品规格列表ids($goods_id);
        }
        $data['goods_sku_price_ids'] = $goods_sku_price_ids;

        $this->验证商品是否含有这个库存规格($goods_id, $goods_sku_price_ids);


        $goods_sku_price_arr = explode(',', $goods_sku_price_ids);
        //判断库存是否充足  发布后需要减少库存
        $res = $this->isGoodsskuPriceStockIds($goods_sku_price_arr);
        if ($res != 'ok') {
            return $this->error('库存异常', '', CODE_库存不足);
        }

        // if ($minimum_price > $max_price) {
        //     return $this->error('保留价不能大于限高价');
        // }

        $data['timer_dec_s'] = $timer_dec_s;
        $data['start_price'] = $start_price;
        $data['now_price'] = $start_price;
        $data['auto_now_price'] = $start_price;
        $data['minimum_price'] = $minimum_price;
        $data['plus_price'] = $plus_price;

        $surety_res = $this->judge_act_surety($this->store_id, $buyer_surety * config('auction_surety_multiple'));
        if (!$surety_res) {
            return $this->error('活动保证金不足', $buyer_surety * config('auction_surety_multiple'), CODE_店铺活动保证金不足);
        }

        if ($start_status == 'number') {
            if ($enroll_num < 1) {
                return $this->error('最低1人开拍');
            }
        }

        $data['dec_price1'] = $dec_price1;
        $data['dec_price2'] = $dec_price2;
        $data['dec_price3'] = $dec_price3;
        $data['dec_price4'] = $dec_price4;

        $data['enroll_num'] = $enroll_num;
        $data['buyer_surety'] = $buyer_surety;
        // $data['out_perk_shell'] = $out_perk_shell;
        $data['passin_plus_price'] = $passin_plus_price;
        $data['enroll_shell'] = $enroll_shell;
        // $data['max_price'] = $max_price;
        $data['auction_advance_show_id'] = $auction_advance_show_id;
        $advance_show_s = Db::name('auction_advance_show')->where('id', '=', $auction_advance_show_id)->value('s');
        $data['auction_end_id'] = $auction_end_id;
        $end_s = Db::name('auction_end')->where('id', '=', $auction_end_id)->value('s');

        $data['advance_show_s'] = $advance_show_s; //预展示多少s
        $data['end_s'] = $end_s;
        // if ($end_time)
        //     $data['end_time'] = strtotime($end_time);
        // $data['start_time'] = $start_time;//开始时间
        $data['cycle'] = $cycle;
        $data['dispatch_ids'] = $dispatch_ids;
        $data['createtime'] = time();


        // $start_time = null, // 开始时间',
        // $end_time = null, // 结束时间',


        $id = Db::name('auction')->insert($data)->getLastInsertId();
        if ($id) {
            // //减少库存


            // foreach ($goods_sku_price_arr as $key => $sku_id) {
            //     $this->DecGoodsStock($sku_id, 1, '发布' . $拍卖类型 . '活动减少库存', $id);
            // }
            //冻结保证金
            $this->DecStoreSurety($this->store_id, $buyer_surety * config('auction_surety_multiple'), '发布' . $拍卖类型 . '活动冻结保证金', $拍卖类型, $id, 'act_surety');
            $this->IncStoreFreeze($this->store_id, 'act_surety', $buyer_surety * config('auction_surety_multiple'), 'store_auction', $id, '发布' . $拍卖类型 . '活动冻结保证金');
        }

        return $this->success('等待审核');
    }



    /**
     * 创建升价拍
     *

     * @param string $start_status 开始类型:time=时间,number=人数,
     * @param int $enroll_num 报名人数'
     * @param int $goods_id 商品',
     * @param string $goods_sku_price_ids 商品规格',
     * @param float $start_price 起拍价格',
     * @param float $minimum_price 最低价格(保留价)',
     * @param float $plus_price 加价幅度',
     * @param float $buyer_surety 买家保证金',
     * @param float $out_perk_shell 出局贝壳补贴',
     * @param float $enroll_out_perk_shell 报名出局贝壳补贴',
     * @param float $enroll_score 报名需要积分',
     * @param float $max_price 最高成交价',
     * @param int $auction_advance_show_id 预展示id
     * @param int $auction_end_id 结束时间id
     * @param string $cycle 几个周期',
     * @param string $dispatch_ids 发货模板'
     * @param int $enroll_num 开拍最低人数'人数必传
     * @return void
     * @Action
     */
    public function create3(
        $type_status = null, // 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        $start_status = null, // 开始类型:time=时间,number=人数,
        $goods_id = null, // 商品',
        $goods_sku_price_ids = null, // 商品规格',
        ?float $start_price = null, // 起拍价格',
        ?float $minimum_price = null, // 最低价格(保留价)',
        ?float $plus_price = null, // 加价幅度',
        ?float $buyer_surety = null, // 买家保证金',
        ?float $out_perk_shell = null, // 出局贝壳补贴',
        ?float $enroll_out_perk_shell = null, // 报名出局贝壳补贴',
        ?float $enroll_score = null, // 报名需要积分',
        ?float $max_price = null, // 最高成交价',
        ?int $auction_advance_show_id = null, //预展示id
        ?int $auction_end_id = null, //结束时间id
        // ?int $advance_show_s = null, // 预展示多少s',
        // ?int $end_s = null, // 结束时长',
        ?int $cycle = null, // 几个周期',
        // ?string $end_time = null,
        ?int $enroll_num = null,
        $dispatch_ids = null // 发货模板',
    ) {
        //     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        // * @param int $advance_show_s 预展示多少s',
        // * @param string $end_time 结束时间'人数必传
        // * @param int $end_s 结束时长',
        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        // $param = $this->request->post();
        $type_status = 3;
        switch ($type_status) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_贝壳竞拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '2':
                $拍卖类型 = '降价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_降价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '3':
                $拍卖类型 = '升价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_升价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_降)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_升)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            default:
                # code...
                break;
        }


        if(($enroll_out_perk_shell+$out_perk_shell)>=$plus_price){
            return $this->error('加价幅度不能小于出局补贴之和');
        }

        //发布

        //判断规格的库存是否都存在

        //判断卖家的保证金 = 买家的保证金*1.5倍(可设置)

        //保留价格

        //发布后需要审核

        $goods = $this->GetGoodsDataById($goods_id);
        if ($goods['store_id'] != $this->store_id) {
            return $this->error('本店没有该商品');
        }
        $data['title'] = $goods['title'];
        $data['store_id'] = $this->store_id;
        $data['type_status'] = $type_status;
        $data['start_status'] = $start_status; //开始类型:time=时间,number=人数
        $data['goods_id'] = $goods_id;
        $data['goodscate_id'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->value('pid');
        if(!$goods_sku_price_ids){
            $goods_sku_price_ids = $this->获取商品规格列表ids($goods_id);
        }
        $data['goods_sku_price_ids'] = $goods_sku_price_ids;

        $this->验证商品是否含有这个库存规格($goods_id, $goods_sku_price_ids);

        $goods_sku_price_arr = explode(',', $goods_sku_price_ids);
        //判断库存是否充足  发布后需要减少库存
        $res = $this->isGoodsskuPriceStockIds($goods_sku_price_arr);
        if ($res != 'ok') {
            return $this->error('库存异常', '', CODE_库存不足);
        }

        if ($minimum_price > $max_price) {
            return $this->error('保留价不能大于限高价');
        }

        $data['start_price'] = $start_price;
        $data['now_price'] = $start_price;

        $data['minimum_price'] = $minimum_price;
        $data['plus_price'] = $plus_price;

        $surety_res = $this->judge_act_surety($this->store_id, $buyer_surety * config('auction_surety_multiple'));
        if (!$surety_res) {
            return $this->error('活动保证金不足', $buyer_surety * config('auction_surety_multiple'), CODE_店铺活动保证金不足);
        }

        if ($start_status == 'number') {
            if ($enroll_num < 1) {
                return $this->error('最低1人开拍');
            }
        }
        $data['enroll_num'] = $enroll_num;
        $data['buyer_surety'] = $buyer_surety;
        $data['out_perk_shell'] = $out_perk_shell;
        $data['enroll_out_perk_shell'] = $enroll_out_perk_shell;
        $data['enroll_score'] = $enroll_score;
        $data['max_price'] = $max_price;
        $data['auction_advance_show_id'] = $auction_advance_show_id;
        $advance_show_s = Db::name('auction_advance_show')->where('id', '=', $auction_advance_show_id)->value('s');
        $data['auction_end_id'] = $auction_end_id;
        $end_s = Db::name('auction_end')->where('id', '=', $auction_end_id)->value('s');

        $data['advance_show_s'] = $advance_show_s; //预展示多少s
        $data['end_s'] = $end_s;
        // if ($end_time)
        //     $data['end_time'] = strtotime($end_time);
        // $data['start_time'] = $start_time;//开始时间
        $data['cycle'] = $cycle;
        $data['dispatch_ids'] = $dispatch_ids;
        $data['createtime'] = time();


        // $start_time = null, // 开始时间',
        // $end_time = null, // 结束时间',


        $id = Db::name('auction')->insert($data)->getLastInsertId();
        if ($id) {
            // //减少库存


            // foreach ($goods_sku_price_arr as $key => $sku_id) {
            //     $this->DecGoodsStock($sku_id, 1, '发布' . $拍卖类型 . '活动减少库存', $id);
            // }
            //冻结保证金
            $this->DecStoreSurety($this->store_id, $buyer_surety * config('auction_surety_multiple'), '发布' . $拍卖类型 . '活动冻结保证金', $拍卖类型, $id, 'act_surety');
            $this->IncStoreFreeze($this->store_id, 'act_surety', $buyer_surety * config('auction_surety_multiple'), 'store_auction', $id, '发布' . $拍卖类型 . '活动冻结保证金');
        }

        return $this->success('等待审核');

        // CREATE TABLE `fa_auction` (
        //     `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        //     `title` varchar(50) DEFAULT NULL COMMENT '拍卖名称',
        //     `store_id` int(10) DEFAULT NULL COMMENT '店铺id',
        //     `type_status` enum('1','2','3','4','5') DEFAULT NULL COMMENT '拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍)',
        //     `start_status` enum('time','number') DEFAULT NULL COMMENT '开始类型:time=时间,number=人数',
        //     `goods_id` int(10) DEFAULT NULL COMMENT '商品',
        //     `goods_sku_price_ids` varchar(255) DEFAULT NULL COMMENT '商品规格',
        //     `start_price` decimal(10,2) DEFAULT NULL COMMENT '起拍价格',
        //     `minimum_price` decimal(10,2) DEFAULT NULL COMMENT '最低价格(保留价)',
        //     `plus_price` decimal(10,2) DEFAULT NULL COMMENT '加价幅度',
        //     `buyer_surety` decimal(10,2) DEFAULT NULL COMMENT '买家保证金',
        //     `out_perk_shell` decimal(10,2) DEFAULT NULL COMMENT '出局贝壳补贴',
        //     `enroll_out_perk_shell` decimal(10,2) DEFAULT NULL COMMENT '报名出局贝壳补贴',
        //     `enroll_score` decimal(10,2) DEFAULT NULL COMMENT '报名需要积分',
        //     `max_price` decimal(10,2) DEFAULT '0.00' COMMENT '最高成交价',
        //     `advance_show_s` int(10) DEFAULT NULL COMMENT '预展示多少s',
        //     `end_s` int(11) DEFAULT NULL COMMENT '结束时长',
        //     `start_time` int(11) DEFAULT NULL COMMENT '开始时间',
        //     `end_time` int(11) DEFAULT NULL COMMENT '结束时间',
        //     `cycle` int(10) DEFAULT '1' COMMENT '几个周期',
        //     `dispatch_ids` varchar(255) DEFAULT NULL COMMENT '发货模板',
        //     `rules` text COMMENT '活动规则',
        //     `createtime` int(11) DEFAULT NULL COMMENT '创建时间',
        //     `updatetime` int(11) DEFAULT NULL COMMENT '更新时间',
        //     `deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
        //     PRIMARY KEY (`id`) USING BTREE
        //   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='拍卖';


    }






    /**
     * 创建暗拍降价拍
     *
     * @param string $start_status 开始类型:time=时间,number=人数,
     * @param int $enroll_num 报名人数'
     * @param int $goods_id 商品',
     * @param string $goods_sku_price_ids 商品规格',
     * @param float $start_price 起拍价格',
     * @param float $minimum_price 最低价格(保留价)',
     * @param float $dec_price1 减幅度
     * @param float $buyer_surety 买家保证金',
     * @param float $enroll_score 报名需要积分',
     * @param int $auction_advance_show_id 预展示id
     * @param int $auction_end_id 结束时间id
     * @param string $cycle 几个周期',
     * @param string $dispatch_ids 发货模板'
     * @param int $enroll_num 开拍最低人数'人数必传
     * @param float $change_price 改价费
     * @return void
     * @Action
     */
    public function create4(
        $start_status = null, // 开始类型:time=时间,number=人数,
        $goods_id = null, // 商品',
        $goods_sku_price_ids = null, // 商品规格',
        ?float $start_price = null, // 起拍价格',
        ?float $minimum_price = null, // 最低价格(保留价)',
        ?float $buyer_surety = null, // 买家保证金',
        ?float $change_price = null, // 改价费',
        // ?float $out_perk_shell = null, // 出局贝壳补贴',
        // ?float $passin_plus_price = null, // 流拍后加价',
        ?float $enroll_score = null, // 报名需要积分',
        // ?float $max_price = null, // 最高成交价',
        ?int $auction_advance_show_id = null, //预展示id
        ?int $auction_end_id = null, //结束时间id
        // ?int $advance_show_s = null, // 预展示多少s',
        // ?int $end_s = null, // 结束时长',
        ?int $cycle = null, // 几个周期',
        // ?string $end_time = null,
        ?int $enroll_num = null,

        ?float $dec_price1 = null,
        // ?float $dec_price2 = null,
        // ?float $dec_price3 = null,
        // ?float $dec_price4 = null,

        $dispatch_ids = null // 发货模板',

    ) {
        //     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        // * @param int $advance_show_s 预展示多少s',
        // * @param string $end_time 结束时间'人数必传
        // * @param int $end_s 结束时长',
        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        // $param = $this->request->post();
        $type_status = 4;
        switch ($type_status) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_贝壳竞拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '2':
                $拍卖类型 = '降价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_降价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '3':
                $拍卖类型 = '升价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_升价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_降)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_升)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            default:
                # code...
                break;
        }

        //发布

        //判断规格的库存是否都存在

        //判断卖家的保证金 = 买家的保证金*1.5倍(可设置)

        //保留价格

        //发布后需要审核

        $goods = $this->GetGoodsDataById($goods_id);
        if ($goods['store_id'] != $this->store_id) {
            return $this->error('本店没有该商品');
        }


        $data['title'] = $goods['title'];
        $data['store_id'] = $this->store_id;
        $data['type_status'] = $type_status;
        $data['start_status'] = $start_status; //开始类型:time=时间,number=人数
        $data['goods_id'] = $goods_id;
        $data['goodscate_id'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->value('pid');
        if(!$goods_sku_price_ids){
            $goods_sku_price_ids = $this->获取商品规格列表ids($goods_id);
        }
        $data['goods_sku_price_ids'] = $goods_sku_price_ids;

        $this->验证商品是否含有这个库存规格($goods_id, $goods_sku_price_ids);


        $goods_sku_price_arr = explode(',', $goods_sku_price_ids);
        //判断库存是否充足  发布后需要减少库存
        $res = $this->isGoodsskuPriceStockIds($goods_sku_price_arr);
        if ($res != 'ok') {
            return $this->error('库存异常', '', CODE_库存不足);
        }

        // if ($minimum_price > $max_price) {
        //     return $this->error('保留价不能大于限高价');
        // }

        // $data['timer_dec_s'] = $timer_dec_s;
        $data['start_price'] = $start_price;
        $data['now_price'] = $start_price;
        // $data['auto_now_price'] = $start_price;
        $data['minimum_price'] = $minimum_price;


        $surety_res = $this->judge_act_surety($this->store_id, $buyer_surety * config('auction_surety_multiple'));
        if (!$surety_res) {
            return $this->error('活动保证金不足', $buyer_surety * config('auction_surety_multiple'), CODE_店铺活动保证金不足);
        }

        if ($start_status == 'number') {
            if ($enroll_num < 1) {
                return $this->error('最低1人开拍');
            }
        }

        $data['dec_price1'] = $dec_price1;
        // $data['dec_price2'] = $dec_price2;
        // $data['dec_price3'] = $dec_price3;
        // $data['dec_price4'] = $dec_price4;

        $data['enroll_num'] = $enroll_num;
        $data['buyer_surety'] = $buyer_surety;
        // $data['out_perk_shell'] = $out_perk_shell;
        // $data['passin_plus_price'] = $passin_plus_price;
        $data['enroll_score'] = $enroll_score;
        $data['change_price'] = $change_price;
        // $data['max_price'] = $max_price;
        $data['auction_advance_show_id'] = $auction_advance_show_id;
        $advance_show_s = Db::name('auction_advance_show')->where('id', '=', $auction_advance_show_id)->value('s');
        $data['auction_end_id'] = $auction_end_id;
        $end_s = Db::name('auction_end')->where('id', '=', $auction_end_id)->value('s');

        $data['advance_show_s'] = $advance_show_s; //预展示多少s
        $data['end_s'] = $end_s;
        // if ($end_time)
        //     $data['end_time'] = strtotime($end_time);
        // $data['start_time'] = $start_time;//开始时间
        $data['cycle'] = $cycle;
        $data['dispatch_ids'] = $dispatch_ids;
        $data['createtime'] = time();


        // $start_time = null, // 开始时间',
        // $end_time = null, // 结束时间',


        $id = Db::name('auction')->insert($data)->getLastInsertId();
        if ($id) {
            // //减少库存


            // foreach ($goods_sku_price_arr as $key => $sku_id) {
            //     $this->DecGoodsStock($sku_id, 1, '发布' . $拍卖类型 . '活动减少库存', $id);
            // }
            //冻结保证金
            $this->DecStoreSurety($this->store_id, $buyer_surety * config('auction_surety_multiple'), '发布' . $拍卖类型 . '活动冻结保证金', $拍卖类型, $id, 'act_surety');
            $this->IncStoreFreeze($this->store_id, 'act_surety', $buyer_surety * config('auction_surety_multiple'), 'store_auction', $id, '发布' . $拍卖类型 . '活动冻结保证金');
        }

        return $this->success('等待审核');
    }




    /**
     * 创建暗拍升价拍
     *

     * @param string $start_status 开始类型:time=时间,number=人数,
     * @param int $enroll_num 报名人数'
     * @param int $goods_id 商品',
     * @param string $goods_sku_price_ids 商品规格',
     * @param float $start_price 起拍价格',
     * @param float $minimum_price 最低价格(保留价)',
     * @param float $plus_price 加价幅度',
     * @param float $buyer_surety 买家保证金',
     * @param float $change_price 改价费',
     * @param float $enroll_score 报名需要积分',
     * @param float $max_price 最高成交价',
     * @param int $auction_advance_show_id 预展示id
     * @param int $auction_end_id 结束时间id
     * @param string $cycle 几个周期',
     * @param string $dispatch_ids 发货模板'
     * @param int $enroll_num 开拍最低人数'人数必传
     * @return void
     * @Action
     */
    public function create5(
        //     * @param float $out_perk_shell 出局贝壳补贴',

        $type_status = null, // 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        $start_status = null, // 开始类型:time=时间,number=人数,
        $goods_id = null, // 商品',
        $goods_sku_price_ids = null, // 商品规格',
        ?float $start_price = null, // 起拍价格',
        ?float $minimum_price = null, // 最低价格(保留价)',
        ?float $plus_price = null, // 加价幅度',
        ?float $buyer_surety = null, // 买家保证金',
        // ?float $out_perk_shell = null, // 出局贝壳补贴',
        ?float $change_price = null, // 改价费',
        ?float $enroll_score = null, // 报名需要积分',
        ?float $max_price = null, // 最高成交价',
        ?int $auction_advance_show_id = null, //预展示id
        ?int $auction_end_id = null, //结束时间id
        // ?int $advance_show_s = null, // 预展示多少s',
        // ?int $end_s = null, // 结束时长',
        ?int $cycle = null, // 几个周期',
        // ?string $end_time = null,
        ?int $enroll_num = null,
        $dispatch_ids = null // 发货模板',
    ) {
        //     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍),
        // * @param int $advance_show_s 预展示多少s',
        // * @param string $end_time 结束时间'人数必传
        // * @param int $end_s 结束时长',
        $this->init();
        $this->isRun(); //是否可以继续以下操作
        $this->isAddGoods();
        // $param = $this->request->post();
        $type_status = 5;
        switch ($type_status) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_贝壳竞拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '2':
                $拍卖类型 = '降价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_降价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '3':
                $拍卖类型 = '升价拍';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_升价拍)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_降)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                if (!$this->GetStoreSetmod($this->store->id, $this->store->set_id, MOD_暗拍_升)) {
                    return $this->error('该等级还不支持' . $拍卖类型, $this->store->set_id);
                }
                break;
            default:
                # code...
                break;
        }

        //发布

        //判断规格的库存是否都存在

        //判断卖家的保证金 = 买家的保证金*1.5倍(可设置)

        //保留价格

        //发布后需要审核

        $goods = $this->GetGoodsDataById($goods_id);
        if ($goods['store_id'] != $this->store_id) {
            return $this->error('本店没有该商品');
        }
        $data['title'] = $goods['title'];
        $data['store_id'] = $this->store_id;
        $data['type_status'] = $type_status;
        $data['start_status'] = $start_status; //开始类型:time=时间,number=人数
        $data['goods_id'] = $goods_id;
        $data['goodscate_id'] = Db::name('goodscate')->where('id', '=', $goods['goodscate_ids'])->value('pid');

        if(!$goods_sku_price_ids){
            $goods_sku_price_ids = $this->获取商品规格列表ids($goods_id);
        }
        $data['goods_sku_price_ids'] = $goods_sku_price_ids;
        $this->验证商品是否含有这个库存规格($goods_id, $goods_sku_price_ids);

        $goods_sku_price_arr = explode(',', $goods_sku_price_ids);
        //判断库存是否充足  发布后需要减少库存
        $res = $this->isGoodsskuPriceStockIds($goods_sku_price_arr);
        if ($res != 'ok') {
            return $this->error('库存异常', '', CODE_库存不足);
        }

        if ($max_price > 0 && $minimum_price > $max_price) {
            return $this->error('保留价不能大于限高价');
        }

        $data['start_price'] = $start_price;
        $data['now_price'] = $start_price;
        // $data['auto_now_price'] = $start_price;
        $data['minimum_price'] = $minimum_price;
        $data['plus_price'] = $plus_price;

        $surety_res = $this->judge_act_surety($this->store_id, $buyer_surety * config('auction_surety_multiple'));
        if (!$surety_res) {
            return $this->error('活动保证金不足', $buyer_surety * config('auction_surety_multiple'), CODE_店铺活动保证金不足);
        }

        if ($start_status == 'number') {
            if ($enroll_num < 1) {
                return $this->error('最低1人开拍');
            }
        }
        $data['enroll_num'] = $enroll_num;
        $data['buyer_surety'] = $buyer_surety;
        // $data['out_perk_shell'] = $out_perk_shell;
        $data['change_price'] = $change_price;
        $data['enroll_score'] = $enroll_score;
        $data['max_price'] = $max_price;
        $data['auction_advance_show_id'] = $auction_advance_show_id;
        $advance_show_s = Db::name('auction_advance_show')->where('id', '=', $auction_advance_show_id)->value('s');
        $data['auction_end_id'] = $auction_end_id;
        $end_s = Db::name('auction_end')->where('id', '=', $auction_end_id)->value('s');

        $data['advance_show_s'] = $advance_show_s; //预展示多少s
        $data['end_s'] = $end_s;
        // if ($end_time)
        //     $data['end_time'] = strtotime($end_time);
        // $data['start_time'] = $start_time;//开始时间
        $data['cycle'] = $cycle;
        $data['dispatch_ids'] = $dispatch_ids;
        $data['createtime'] = time();


        // $start_time = null, // 开始时间',
        // $end_time = null, // 结束时间',


        $id = Db::name('auction')->insert($data)->getLastInsertId();
        if ($id) {
            // //减少库存


            // foreach ($goods_sku_price_arr as $key => $sku_id) {
            //     $this->DecGoodsStock($sku_id, 1, '发布' . $拍卖类型 . '活动减少库存', $id);
            // }
            //冻结保证金
            $this->DecStoreSurety($this->store_id, $buyer_surety * config('auction_surety_multiple'), '发布' . $拍卖类型 . '活动冻结保证金', $拍卖类型, $id, 'act_surety');
            $this->IncStoreFreeze($this->store_id, 'act_surety', $buyer_surety * config('auction_surety_multiple'), 'store_auction', $id, '发布' . $拍卖类型 . '活动冻结保证金');
        }

        return $this->success('等待审核');
    }




    /**
     * 获取我的拍卖列表
     *
     * @param integer $page 页1
     * @param integer $limit 数量10
     * @param string $search 搜索
     * @param string $type_status 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍)
     * @param string $status 状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架    
     * @param string $apply_status 申请状态:1=待审核,2=通过,3=拒绝
     * @return void
     * @Action
     */
    public function get_auction_list(?int $page = 1, ?int $limit = 10, $apply_status = null, $search = null, $type = null, $status = null, $type_status = null)
    {
        $this->init();

        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where['store_id'] = $this->store_id;
        if ($apply_status) {
            //申请状态:1=待审核,2=通过,3=拒绝
            // if ($apply_status == '4') {
            //     $where['apply_status'] = ['in', ['0', '1', '3']];
            // } else {
            $where['apply_status'] = $apply_status;
            // }
        }

        // if ($search)
        //     $where['title|subtitle|content'] = ['like', '%' . $search . '%'];
        if ($type)
            $where['type'] = $type;
        if ($status)
            $where['status'] = $status;
        if ($type_status)
            $where['type_status'] = $type_status;

        $list = Db::name('auction')

            // ->whereEx($this->whereCom)

            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->orderRaw('createtime desc')
            ->page($page, $limit)
            ->select()
            ->getArray();
        if ($list) {
            foreach ($list as $key => &$val) {
                $this->ChangGoodsField($val);
            }
        }
        return $this->success('', $list);
    }






    protected function ChangGoodsField($auction)
    {
        $goods = $this->GetGoodsDataById($auction['goods_id']);
        $auction['goods'] = $goods;

        // $auction['goodscate'] = Db::name('goodscate')->where('id', '=', $auction['goodscate_ids'])->find();
        // $auction['p_goodscate'] = Db::name('goodscate')->where('id', '=', $auction['goodscate']['pid'])->find();
        // if ($auction['new_status'] == 'soon')
        //     $auction['expire_date'] = date('Y-m-d H:i', $auction['expire_time']);
        $auction['goodssku_price_list'] = GoodsskuPrice::dbQuery()->whereIn('id', explode(',', $auction['goods_sku_price_ids']))->select()->getArray();


        return $auction;
    }




    /**
     *获取商品是否有足够的数量
     *
     * @param int|null $goods_sku_price_ids 规格ids
     * @param int $num 数量1
     * @return void
     */
    protected function isGoodsskuPriceStockIds($goods_sku_price_ids, $num = 1)
    {

        $list = Db::name('goodssku_price')
            ->whereIn('id', $goods_sku_price_ids)
            // ->cache($this->db_cache_set)
            ->select()->getArray();

        if (!$list) {
            $this->error('暂无该规格', $goods_sku_price_ids, CODE_暂无该规格);
            throw new \Exception('库存ids是否正确');
        }
        foreach ($list as $key => $goodssku_price) {
            # code...
            $goods = $this->GetGoodsDataById($goodssku_price['goods_id']); //Goods::find(['id'=>$goodssku_price['goods_id']]);
            if (!$goods) {
                return $this->error('该商品不存在', $goodssku_price['goods_id'], CODE_该商品不存在);
            }
            if ($goods['status'] == 'down') {
                return $this->error('该商品已下架', null, CODE_该商品已下架);
            }
            if ($goodssku_price['status'] == 'down') {
                return $this->error('该规格已下架', $goods_sku_price_id, CODE_该规格已下架);
            }
            if ($goodssku_price['stock'] < $num) {
                return $this->error($goods['title'] . '⭐️库存不足', $goodssku_price, CODE_库存不足);
            }
        }
        return 'ok';
    }



    // /**
    //  * 编辑商品
    //  *
    //  * @param int $id
    //  * @param string $title 商品标题
    //  * @param string $subtitle 副标题
    //  * @param string $goodscate_ids 商品分类id
    //  * @param string $content 商品详情介绍
    //  * @param string $dispatch_ids 发货模板
    //  * @param string $dispatch_type 发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销'
    //  * @param string $image 商品主图
    //  * @param string $images 商品图集
    //  * @param string $content_images 商品详细图集
    //  * @param string $is_sku 是否多规格0=否1=是
    //  * @param string $params 规格参数[{"title":"品牌","content":"xiaomi"},{"title":"产地","content":"中国"}]
    //  * @param string $original_price 原价
    //  * @param string $price 价格
    //  * @param string $type 商品类型:normal=实体商品,virtual=虚拟商品,service=服务
    //  * @param string $show_sales 显示销量
    //  * @param string $service_ids 服务标签id
    //  * @param string $video_image 视频封面
    //  * @param string $video_file 视频文件
    //  * @param string $goodsbrand_id 品牌id
    //  * @param string $send_addr 发货地址
    //  * @param string $expire_time 到期时间
    //  * @param string $new_status 商品类型:new=全新,unused=闲置,soon=临期
    //  * @param string $listJson 规格参数[{id,name,pid,weigh,temp_id,children:[id,name,pid,weigh,temp_id]}]
    //  * @param string $priceJson 价格参数{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]}

    //  * @return void
    //  * @Action
    //  */
    // public function edit_goods(
    //     $id,
    //     $title = null,
    //     $subtitle = null,
    //     $goodscate_ids = null,
    //     $content = null,
    //     $dispatch_ids = null,
    //     $dispatch_type = 'express',
    //     $image = null,
    //     $images = null,
    //     $content_images = null,
    //     $is_sku = null,
    //     $params = null,
    //     $original_price = null,
    //     $price = null,
    //     $type = null,
    //     $show_sales = null,
    //     $stock = null,
    //     $sn = null,
    //     $listJson = null,
    //     $priceJson = null,
    //     $service_ids = null,
    //     $video_image = null,
    //     $video_file = null,
    //     $goodsbrand_id = null,
    //     $send_addr = null,
    //     $expire_time = null,
    //     $new_status = null
    // ) {
    //     $this->init();
    //     $this->isRun(); //是否可以继续以下操作

    //     $where['id'] = $id;
    //     $where['store_id'] = $this->store_id;
    //     $goods = Db::name('goods')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)->select()->get();
    //     if (!$goods) {
    //         return $this->error('没有该商品');
    //     }

    //     $param = $this->request->param();
    //     if (!$goods['is_sku']) {
    //         // 单规格，price 必须是数字
    //         if (!preg_match('/^[0-9]+(.[0-9]{1,8})?$/', $param['price'])) {
    //             return $this->error("请填写正确的价格");
    //         }
    //     }

    //     if ($goodscate_ids)
    //         $this->isGoodscateIds($goodscate_ids); //判断店铺是否有该商品的资格
    //     if ($goodscate_ids)
    //         $data['goodscate_ids'] = $goodscate_ids;
    //     if ($goodsbrand_id)
    //         $data['goodsbrand_id'] = $goodsbrand_id;
    //     if ($send_addr)
    //         $data['send_addr'] = $send_addr;
    //     if ($content)
    //         $data['content'] = $this->Xss($content);
    //     if ($content_images)
    //         $data['content_images'] = $content_images;

    //     if ($dispatch_ids)
    //         $data['dispatch_ids'] = $dispatch_ids; //发货模板
    //     if ($dispatch_type)
    //         $data['dispatch_type'] = $dispatch_type; //发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销',
    //     // $data['expire_day'] = '0';
    //     if ($image)
    //         $data['image'] = $image;
    //     if ($images)
    //         $data['images'] = $images;

    //     // $data['is_sku'] = $is_sku; //是否多规格 0=否1=是
    //     if ($original_price)
    //         $data['original_price'] = $original_price; //原价
    //     if ($params)
    //         $data['params'] = $this->Xss($params);
    //     // $data['params_arr][0][title'] = '品牌';
    //     // $data['params_arr][0][content'] = 'iphone';
    //     // $data['params_arr][1][title'] = '产地';
    //     // $data['params_arr][1][content'] = '中国';
    //     if ($price)
    //         $data['price'] = $price; //价格
    //     if ($service_ids)
    //         $data['service_ids'] = $service_ids; //服务标签
    //     // if($show_sales)
    //     // $data['show_sales'] = $show_sales; //显示销量
    //     // $data['status'] = 'up';
    //     if ($subtitle)
    //         $data['subtitle'] = $this->Xss($subtitle);
    //     if ($title)
    //         $data['title'] = $this->Xss($title);
    //     if ($type)
    //         $data['type'] = $type; //商品类型
    //     // $data['views'] = '10'; //浏览人数
    //     // $data['weigh'] = '999999';
    //     // $data['weight'] = 0;
    //     // $data['stock'] = $stock;
    //     // $data['stock_warning_switch'] = 'false';
    //     // $data['sn'] = $sn;
    //     // $data['autosend_content'] = '';
    //     // $data['store_id'] = $this->store_id; //店铺ID;
    //     // $data['createtime'] = time();
    //     $data['updatetime'] = time();
    //     $data['apply_status'] = '1'; //审核状态:0=未知,1=申请中,2=已通过,3=已拒绝

    //     if ($video_file)
    //         $data['video_file'] = $video_file;
    //     if ($video_file && !$video_image) {
    //         return $this->error('请上传视频封面', null,);
    //     }
    //     if ($video_image)
    //         $data['video_image'] = $video_image;
    //     $data['new_status'] = $new_status;
    //     if ($expire_time)
    //         $data['expire_time'] = $expire_time;




    //     Db::getInstance()->beginTransaction();
    //     try {
    //         $data['goods_service_ratio'] = $this->StoreGetGoodsMoneyServerRatio($data, $this->store->set_id, $this->store_id); //卖家服务费比率


    //         $data['id'] = $id;
    //         // unset($data['id']);

    //         Db::name('goods')->where('id', '=', $id)->update($data); //->strict(false)
    //         // $data['id'] = $id;
    //         // var_dump('475', $data);
    //         $sku['listJson'] = $listJson;
    //         $sku['priceJson'] = $priceJson;
    //         // $sku['listJson'] = '[{"id":138,"name":"测试颜色","pid":0,"goods_id":40,"weigh":0,"children":[{"id":139,"name":"测试黑色","pid":138,"goods_id":40,"weigh":0,"temp_id":2},{"id":140,"name":"白色","pid":138,"goods_id":40,"weigh":0,"temp_id":3}],"temp_id":1},{"id":141,"name":"容量","pid":0,"goods_id":40,"weigh":0,"children":[{"id":142,"name":"256G","pid":141,"goods_id":40,"weigh":0,"temp_id":5},{"id":143,"name":"128G","pid":141,"goods_id":40,"weigh":0,"temp_id":6}],"temp_id":4}]';
    //         // $sku['priceJson'] = '[{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]},{"id":142,"goods_sku_ids":"140,142","goods_id":40,"weigh":0,"image":"","stock":20,"stock_warning":null,"sales":0,"sn":"A2","weight":160,"price":"2.00","goods_sku_text":["白色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":2,"goods_sku_temp_ids":[3,5]},{"id":143,"goods_sku_ids":"139,143","goods_id":40,"weigh":0,"image":"","stock":30,"stock_warning":null,"sales":0,"sn":"A3","weight":160,"price":"3.00","goods_sku_text":["黑色","128G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":3,"goods_sku_temp_ids":[2,6]},{"id":144,"goods_sku_ids":"140,143","goods_id":40,"weigh":0,"image":"","stock":4,"stock_warning":null,"sales":0,"sn":"A4","weight":160,"price":"4.00","goods_sku_text":["白色","128G"],"status":"down","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":4,"goods_sku_temp_ids":[3,6]}]';
    //         if ($is_sku) {
    //             $data['is_sku'] = $is_sku;
    //         }
    //         $this->editSku($data, $sku, 'edit');

    //         Db::getInstance()->commit();
    //     } catch (\Throwable $e) {
    //         Db::getInstance()->rollBack();
    //         return $this->error($e->getMessage());
    //     }
    //     return $this->success('编辑成功,等待审核', $data);
    // }

    // /**
    //  * 更新商品状态
    //  *
    //  * @param int $id 商品id
    //  * @param string $status 状态up=上架,hidden=隐藏商品,down=下架
    //  * @return void
    //  * @Action
    //  */
    // public function update_goods_status($id, $status)
    // {
    //     $this->init();
    //     $this->isRun(); //是否可以继续以下操作

    //     $where['id'] = $id;
    //     $where['store_id'] = $this->store_id;
    //     $goods = Db::name('goods')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)
    //         ->select()
    //         ->get();
    //     if (!$goods) {
    //         return $this->error('没有该商品');
    //     }
    //     switch ($status) { //商品状态:up=上架,hidden=隐藏商品,down=下架
    //         case 'up':
    //             // 上架 判断是否审核通过
    //             if ($goods['apply_status'] != '2')
    //                 return $this->error('审核状态未通过');
    //             break;
    //         case 'hidden':
    //             //隐藏商品 
    //             break;
    //         case 'down':
    //             //下架 判断云仓什么的
    //             break;
    //         default:
    //             # code...
    //             break;
    //     }
    //     $data['status'] = $status;

    //     Db::name('goods')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)->update($data);
    //     return $this->error('修改成功');
    // }

    // /**
    //  * 获取商品详情
    //  *
    //  * @param int $id 商品id
    //  * @return void
    //  * @Action
    //  */
    // public function get_goods_detail(?int $id)
    // {
    //     $this->init();
    //     $where['id'] = $id;
    //     $where['store_id'] = $this->store_id;
    //     $goods = Db::name('goods')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)
    //         ->select()
    //         ->get();
    //     if ($goods)
    //         $goods = $this->ChangGoodsField($goods);
    //     return $this->success('goods', $goods);
    // }

    // /**
    //  * 删除商品
    //  *
    //  * @param string|int $id 商品id
    //  * @return void
    //  * @Action
    //  */
    // public function del_goods($id)
    // {
    //     $this->init();
    //     $this->isRun(); //是否可以继续以下操作

    //     if (is_numeric($id)) {
    //         $where['id'] = $id;
    //     } else {
    //         $where['id'] = ['in', explode(',', $id)];
    //     }
    //     $where['store_id'] = $this->store_id;
    //     $list = Db::name('goods')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)->whereIsNull('deletetime')->select()->getArray();
    //     if (!$list) {
    //         return $this->error('没有该商品');
    //     }
    //     foreach ($list as $key => $goods) {
    //         $where2['id'] = $goods['id'];

    //         $res = Db::name('goods')
    //             // ->whereEx($this->whereCom)
    //             ->whereEx($where2)->update(['deletetime' => time(), 'status' => 'down']);
    //     }
    //     //删除判断 云仓等数据

    //     return $this->success('删除成功');
    // }


}
